package anc.tankshooter.network.ui;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import anc.tankshooter.GameActivity;
import anc.tankshooter.controls.InputText;
import anc.tankshooter.ui.IScreen;
import anc.tankshooter.ui.ResourcesManager;
import anc.tankshooter.ui.SceneManager.SceneType;
import anc.tankshooter.ui.SettingScreen.SettingButtonListener;

/**
 * Man hinh dang nhap SERVER
 * @author tauitdnmd
 *
 */
public class LoginScreen extends IScreen {

	private SettingButtonListener mlistener;
	private Rectangle mBackgroundRect;
	private Sprite mBackgroundBox;
	private Text mMessageText;
	private InputText mUsername,mPassword;
	private TiledSprite mBtnConnect,mBtnCancle;
	
	public static String mUser,mPass;
	private boolean mIsRunning;
	
	public LoginScreen(SettingButtonListener l){
		mlistener = l;
	}
	@Override
	protected void createScene() {
		mBackgroundRect = new Rectangle(0, 0, GameActivity.CAMERA_WITDH, GameActivity.CAMERA_HEIGHT,ResourcesManager.getInstance().mVertexManager);
		mBackgroundRect.setColor(Color.BLACK); mBackgroundRect.setAlpha(0.6f);
		float x = GameActivity.CAMERA_WITDH/2 - ResourcesManager.getInstance().mLoginBackgroundTexture.getWidth()/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - ResourcesManager.getInstance().mLoginBackgroundTexture.getHeight()/2;
		mBackgroundBox = new Sprite(x, y, ResourcesManager.getInstance().mLoginBackgroundTexture, mVertextManager);
		
		
		mMessageText = new Text(260,y+40, ResourcesManager.getInstance().mWhiteFont18, "", 256,ResourcesManager.getInstance().mVertexManager);
		mUsername = new InputText(260f,y+100, "Your username",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,20);
		
		
		mPassword = new InputText(260f, y+190, "Your password",ResourcesManager.getInstance().mTextBoxTexture, ResourcesManager.getInstance().mWhiteFont20,20);
		mPassword.setPassword(true);
		
		mBtnConnect = new TiledSprite(300,y+240,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnConnect.setCurrentTileIndex(10);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnConnect.setCurrentTileIndex(11);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					mBtnConnect.setCurrentTileIndex(10);
					doLogin();
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnConnect.setCurrentTileIndex(10);
		
		mBtnCancle = new TiledSprite(300 + ResourcesManager.getInstance().mBtnTexture.getWidth()+10,y+240,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnCancle.setCurrentTileIndex(8);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnCancle.setCurrentTileIndex(9);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					mBtnCancle.setCurrentTileIndex(8);
					if(mlistener!=null)
						mlistener.onCancleClicked();
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mBtnCancle.setCurrentTileIndex(8);
		
		attachChild(mBackgroundRect);
		attachChild(mBackgroundBox);
		attachChild(mMessageText);
		registerTouchArea(mUsername);
		attachChild(mUsername);
		registerTouchArea(mPassword);
		attachChild(mPassword);
		registerTouchArea(mBtnConnect);
		attachChild(mBtnConnect);
		
		registerTouchArea(mBtnCancle);
		attachChild(mBtnCancle);
	}

	@Override
	public boolean onBackKeyPressed() {
		
		return true;
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_LOGIN;
	}

	@Override
	public void disposeScene() {
		
	}

	@Override
	public void onUpdateScreen(float second) {		
	}

	public void showMessageText(String s){
		mMessageText.setText(s);
	}
	private void doLogin(){
		if(!mIsRunning) {
			mBtnConnect.setCurrentTileIndex(11);
			mMessageText.setText("Logging in...");
			mUser = mUsername.getText();
			mPass = mPassword.getText();
			
			Thread th = new Thread(new Runnable() {
				@Override
				public void run() {
					mIsRunning = true;
					mUsername.setEnable(false);
					mUsername.setEnable(false);
					
					if(!GameActivity.mClient.sendLogin(mUser,mPass)) {
						mMessageText.setText("The request was not sent.");
					}
					mMessageText.setText("");
					mUsername.setEnable(true);
					mUsername.setEnable(true);
					mBtnConnect.setCurrentTileIndex(10);
					mIsRunning = false;
				}
			});
			th.start();
		}
			
	}
}
